package org.future.task.core.util;

import java.util.concurrent.*;

/**
 * @author Dongwei-Chen
 * @Date 2020/2/20 16:48
 * @Description 创建默认线程池
 */
public class ThreadPoolUtils {
    public ThreadPoolExecutor getDefaultThreadPool(String poolType, int corePoolSize) {
        return new ThreadPoolExecutor(
                corePoolSize, corePoolSize, 3L, TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(),
                new Factory(poolType),
                new ThreadPoolExecutor.DiscardOldestPolicy());
    }

    static class Factory implements ThreadFactory {

        private final String poolType;

        private int counter = 0;

        public Factory(String poolType) {
            this.poolType = poolType;
        }

        @Override
        public Thread newThread(Runnable r) {
            return new Thread(r, "pool-" + poolType + "-" + counter++);
        }

    }
}
